package com.lant.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lant.enums.ResponseStatusEnum;
import com.lant.pojo.LantResult;
import com.lant.service.RedisService;
import com.lant.utils.ExceptionUtil;

import io.swagger.annotations.Api;
import redis.clients.jedis.JedisCluster;

/**
 * @description: 缓存服务实现
 * @projectName: lant-redis-provider
 * @className: RedisServiceImpl
 * @author: YFL
 * @createTime: 2020年4月29日 上午11:07:41
 */
// http://localhost:8080/lant/redis/swagger-ui.html
@RestController
@Api(value = "redis缓存接口")
public class RedisServiceImpl implements RedisService {
	private static final Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);
	@Autowired
	private JedisCluster jedisCluster;

	// @ApiOperation(value ="通过key获取缓存",tags = "通过key获取缓存",notes = "通过key获取缓存")
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	@Override
	public LantResult get(String key) {
		String value = "";
		try {
			value = jedisCluster.get(key);
		} catch (Exception e) {
			logger.error("get缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}

		return LantResult.ok(value);
	}

	@RequestMapping(value = "/set", method = RequestMethod.POST)
	@Override
	public LantResult set(String key, String value) {
		try {
			jedisCluster.set(key, value);
		} catch (Exception e) {
			logger.error("set缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}
		return LantResult.ok();
	}

	@RequestMapping(value = "/setTime", method = RequestMethod.POST)
	@Override
	public LantResult set(String key, String value, int second) {
		try {
			jedisCluster.set(key, value);
			jedisCluster.expire(key, second);
		} catch (Exception e) {
			logger.error("set缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}
		return LantResult.ok();
	}

	@RequestMapping(value = "/hget", method = RequestMethod.POST)
	@Override
	public LantResult hget(String hkey, String key) {
		String value = "";
		try {
			value = jedisCluster.hget(hkey, key);
		} catch (Exception e) {
			logger.error("hget缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}
		return LantResult.ok(value);
	}

	@RequestMapping(value = "/hset", method = RequestMethod.POST)
	@Override
	public LantResult hset(String hkey, String key, String value) {
		try {
			jedisCluster.hset(hkey, key, value);
		} catch (Exception e) {
			logger.error("hset缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}
		return LantResult.ok();
	}

	@RequestMapping(value = "/incr", method = RequestMethod.POST)
	@Override
	public LantResult incr(String key) {
		try {
			jedisCluster.incr(key);
		} catch (Exception e) {
			logger.error("incr缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}
		return LantResult.ok();
	}

	@RequestMapping(value = "/expire", method = RequestMethod.POST)
	@Override
	public LantResult expire(String key, int seconds) {
		try {
			jedisCluster.expire(key, seconds);
		} catch (Exception e) {
			logger.error("expire缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}
		return LantResult.ok();
	}

	@RequestMapping(value = "/ttl", method = RequestMethod.POST)
	@Override
	public LantResult ttl(String key) {
		Long ttl = 0L;
		try {
			ttl = jedisCluster.ttl(key);
		} catch (Exception e) {
			logger.error("ttl缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}

		return LantResult.ok(ttl);
	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@Override
	public LantResult del(String key) {
		try {
			jedisCluster.del(key);
		} catch (Exception e) {
			logger.error("del缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}

		return LantResult.ok();
	}

	@RequestMapping(value = "/hdel", method = RequestMethod.POST)
	@Override
	public LantResult hdel(String hkey, String key) {
		try {
			jedisCluster.hdel(hkey, key);
		} catch (Exception e) {
			logger.error("hdel缓存失败", e.getMessage());
			return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), ExceptionUtil.getStackTrace(e));
		}

		return LantResult.ok();
	}
}
